var xfglid = "";
$(document).ready(function(){  
	$("#functionNav").attr("href",navAddr);
	//获取查询条件
	var id =GetQueryString("id");
	xfglid  = id;
	var cpInfo = ajaxResult("get",false,ProjectName+"/colrepair.getColRepairDetailById.do",{'id':id});
	//填充藏品信息到表单
	if(cpInfo.status=="ok"){
		loadData("#form1",cpInfo.data); 
		if(cpInfo.data.XFYQ==""){
			$("#XFYQ").val(cpInfo.data.XFZTYQ); 
		}
		if(cpInfo.data.editable==true){//可编辑
			$("#div_button").show();
			$("#div_ysDownload").hide();
			$("#createRecord").show(); 
			$('#gsgs').removeAttr("readonly");
		}else{//只读
			$("#div_button").hide();
			$("#div_ysDownload").show();
			$("#createRecord").hide();
			$('#gsgs').attr("readonly","readonly");
		}
	}
    //初始化出退库单据Table
	createTable('#repairTable',ProjectName+'/colrepair.getRepairRecordList.do','id',xfjlTitle,{"xfglid":id});	
	//点击查询按钮
	 $("#search_btn").click(function(){
		 refreshTable('#repairTable',{"xfglid":id});
	 });

	 // 下发具体修复人
	 $("#xfjtxfr_btn").click(function(){
		 var xfrid = $("#xfjtxfr").val();
		 var xfrmc = $("#xfjtxfrText").val();
		 if(xfrid==""){
			 alertFunc("请选择具体修复人");
			 return;
		 }
		 var params = {"xfglid":id,"xfrid":xfrid,"xfrmc":xfrmc};
		 var detail = ajaxResult("get",false,ProjectName+"/colrepair.createRepairRecord.do",params);
		 //填充藏品信息到表单
		 if(detail.status=="ok"){
			 alertFunc("下发成功！");
			 $("#xfjtxfr").val("");
			 $("#xfjtxfrText").val("");
			 refreshTable('#repairTable',{"xfglid":id});
		 }else{
			 alertFunc("下发失败，请重试！");
		 }
	 });
	 
	// 验收
	 $("#ys_btn").click(function(){
		 var detail = ajaxResult("get",false,ProjectName+"/colrepair.validRepairRecordComplete.do",{"xfglid":xfglid});
		 //填充藏品信息到表单
		 if(detail.status=="ok"){
			 var n = detail.data.n;
			 if(n==0){
				 $("#ysModal").modal('show');
				 return;
			 }
		 }
		 alert("还有修复没有完成，请等待，详细信息请查看修复记录列表！");
	 });
	// 验收
	 $("#mod_ys_btn").click(function(){
		 if(checkform("#form_ys")){
			 var check= true;
			    $("#form_ys input[id$=Check]").each(function(){
					if($(this).val()=='0'){
						alertFunc("用户验证未通过！");
						check = false;
						return false;
					}
				});
			    if(!check)
			    	return false;
			    
		 var params = $("#form_ys").serializeObject();
		 params.id=xfglid;
		 params.zeRenBaoGuanYuan = $("#zeRenBaoGuanYuan").val();
		 params.xiuFuFuZeRen = $("#xiuFuFuZeRen").val();
		 params.buMenZhuRen = $("#buMenZhuRen").val();
		 params.wenBaoFuZeRen = $("#wenBaoFuZeRen").val();
		 params.zeRenBaoGuanYuanName = $("#zeRenBaoGuanYuan option:checked").text()=="请选择" ? "" : $("#zeRenBaoGuanYuan option:checked").text();
		 params.buMenZhuRenName = $("#buMenZhuRen option:checked").text()=="请选择" ? "" : $("#buMenZhuRen option:checked").text();
		 params.wenBaoFuZeRenName = $("#wenBaoFuZeRen option:checked").text()=="请选择" ? "" : $("#wenBaoFuZeRen option:checked").text();
		 params.xiuFuFuZeRenName = $("#xiuFuFuZeRen option:checked").text()=="请选择" ? "" : $("#xiuFuFuZeRen option:checked").text();
		 params.wpzj = $("#wpzj").val();
		 var alertMsg = "您的验收信息为：部门主任："+params.buMenZhuRenName+",文保负责人："+params.wenBaoFuZeRenName+",库房保管员："+params.zeRenBaoGuanYuanName+",修复负责人："+params.xiuFuFuZeRenName+",是否确认提交？";
		 bootbox.confirm(alertMsg, function(result){
				if(result == true){
					var detail = ajaxResult("get",false,ProjectName+"/colrepair.closeRepair.do",params);
					 //填充藏品信息到表单
					 if(detail.status=="ok"){
						 alertFunc("验收成功！");
						 $("#ysModal").modal('hide');
						 window.location="cpxfgl.html";
					 }else{
						 alertFunc("验收失败！");
					 }
				}
			});
		 }
		 
	 });
	 getUserRight();
	// 通知申请人
	 $("#sendmsgtosqr_btn").click(function(){
		 var msg = "您申请的藏品已经修复完成，请与修复部门联系验收！";
		 var  userids = $("#SQRID").val();
		 var creator = userInfo.username;
		 var isurl = 1;
		 var orgname = $("#SQBMMC").val();
		 var msgObj = {'msg':msg,'userids':userids,'creator':creator,'isurl':isurl,'orgname':orgname};
		 $.ajax({
			 type:"post",
			 dataType: 'text',
			 async:false,
			 url:palaceAddr+"/msg.addMsgForEx.do",
			 data:msgObj,
			 success:function(data){				
			  if(data=="success"){
				  alertFunc("发送消息成功!");
			  }else{
				  alertFunc("发送消息失败!");
			  }
			 }
		 });	
	 });

	// 保存修复记录
	 $("#mod_savexfjl_btn").click(function(){
		 var params = $("#form_xfjldetail").serializeObject();
		 params.id=$("#xfjlid").val();
		 var detail = ajaxResult("get",false,ProjectName+"/colrepair.saveRepairRecord.do",params);
		 //填充藏品信息到表单
		 if(detail.status=="ok"){
			 alertFunc("保存成功！");
			 refreshTable('#repairTable',{"xfglid":id});
		 }else{
			 alertFunc("保存失败！");
		 }
	 });
	// 结束修复记录
	 $("#mod_closexfjl_btn").click(function(){
		 var val = new validate({
  			rules:{	
  				xflx:"notEmpty"
  			},
  			submitFun:function(){      				     				
  				 bootbox.setLocale("zh_CN");
  				 bootbox.confirm("您确定结束修复？",function(r){
  					 if(r){
  						 var params = $("#form_xfjldetail").serializeObject();
  						 params.id=$("#xfjlid").val();
  						 var detail = ajaxResult("get",false,ProjectName+"/colrepair.saveRepairRecord.do",params);
  						 if(detail.status=="ok"){  							 
  							 detail = ajaxResult("get",false,ProjectName+"/colrepair.closeRepairRecord.do",params);
  							 //填充藏品信息到表单
  							 if(detail.status=="ok"){
  								 alertFunc("成功！");
  								 $("#cpxfjldetailModal").modal('hide');
  								 refreshTable('#repairTable',{"xfglid":id});
  							 }else{
  								 alertFunc("失败！");
  							 }
  						 }
  					 }
  				 });					    									     				
  			}
  		});
	 });
	 // 填充验收下拉列表
	 // 送修部门主任
	 var buMenZhuRenJson = ajaxResult("get",false,ProjectName+"/root.getDirectorList.do",{'orgId':cpInfo.data.SQBMID,'pos':'主任'});
	 loadSelect("select[name=buMenZhuRen]",buMenZhuRenJson.rows,'userid','username');
	// 文保负责人
	 var wenBaoFuZeRenJson = ajaxResult("get",false,ProjectName+"/root.getUserListByRole.do",{'roleId':'xf_wbfzr'});
	 loadSelect("select[name=wenBaoFuZeRen]",wenBaoFuZeRenJson.rows,'userid','username');
	// 库房责任保管员
	 var zeRenBaoGuanYuanJson = ajaxResult("get",false,ProjectName+"/sysmanage.getBgyByKfId.do",{'kfid':cpInfo.data.szkf});
	 loadSelect("select[name=zeRenBaoGuanYuan]",zeRenBaoGuanYuanJson.rows,'userid','username');
	// 修复负责人
	 var xiuFuFuZeRenJson = ajaxResult("get",false,ProjectName+"/root.getUserListByOrg.do",{'orgId':'wenwubaowubu'});
	 loadSelect("select[name=xiuFuFuZeRen]",xiuFuFuZeRenJson.rows,'userid','username');
	// 下载验收报告
	 $("#ysDownload_btn").click(function(){
		  var params = "id="+xfglid;
		  window.location.href=ProjectName+"/colrepair.ysDownload.do?"+params;
	  });
})

function dealSend(data,t_dom){
	if(data.status){    
		alertFunc('消息发送成功');
	}else{
		alertFunc('消息发送失败');
	}
}
function changeGsgs(){
	var gsgs = $("#gsgs").val();
	var detail = ajaxResult("get",false,ProjectName+"/colrepair.saveRepair.do",{'id':xfglid,"gsgs":gsgs});
}
function opencpxfjldetail(id){
	var detail = ajaxResult("get",false,ProjectName+"/colrepair.getColRepairRecordDetailById.do",{'id':id});
	//填充藏品信息到表单
	if(detail.status=="ok"){
		loadData("#form_xfjldetail",detail.data); 
		if(detail.data.editable==true){//可编辑
			$("#mod_closexfjl_btn").show();
			$("#mod_savexfjl_btn").show();
			$("#xfjl_enddate").hide();
			$('#ssgc').removeAttr("readonly");
			$('#sycl').removeAttr("readonly");
			$('#xflx').removeAttr("disabled");
		}else{//只读
			$("#mod_closexfjl_btn").hide();
			$("#mod_savexfjl_btn").hide();
			$('#ssgc').attr("readonly","readonly");
			$('#sycl').attr("readonly","readonly");
			$('#xflx').attr("disabled","disabled");
			$("#xfjl_enddate").show();
		}
	}
	var xflxJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInJb.do",{'tableName':'jb_xflx'});
	loadSelect("#form_xfjldetail select[name=xflx]",xflxJson.rows,'id','name');
	$("#form_xfjldetail select[name=xflx]").val(detail.data.xflx);
	
	$("#xfjlid").val(id);
	$("#cpxfjldetailModal").modal('show');
}
function editCYRY(){
	var params = new Object();
	params["id"] = xfglid;
	params["cyry"] = $("#cyry").val();
	var detail = ajaxResult("get",false,ProjectName+"/colrepair.saveCYRY.do",params);
		 if(detail.status=="ok"){
			 alertFunc("参与人员修改成功！");
		 } else {
			 alertFunc("参与人员修改失败！");
		 }
}
//验证用户名密码是否有效
//验证用户名密码是否有效
function validateUser(formid,input1,input2){
	var userid = $(formid+" "+input1).val();
	var password = $(formid+" "+input2).val();
	if(userid!=''){
		var params = new Object();
		params["userid"] = userid;	
		params["password"] = password;
		var result = ajaxResult("get",false,ProjectName+'/root.validateUser.do',params);
		if(result.status == "ok"){
			$(formid+" "+input2).next("span").remove();
			$(formid+" "+input2).after("<span name='validateText' style='margin-left:5px;'>通过</span>");
			$(formid+" "+input1+"Check").val("1");
		}else{
			$(formid+" "+input2).next("span").remove();
			$(formid+" "+input2).after("<span name='validateText' style='color:red;margin-left:5px;'>不通过</span>");
			$(formid+" "+input1+"Check").val("0");
		}
	}
}
var xfjlTitle = [{
    field: 'xfrmc',
    title: '修复人',
    formatter: function(value, row, index) {
    	return '<a href="#" onclick="opencpxfjldetail('+row.id+')">'+value+'</a>';
	}
},{
    field: 'xflx',
    title: '修复类型',
    formatter: function(value, row, index) {
    	return '<a href="#" onclick="opencpxfjldetail('+row.id+')">'+value+'</a>';
	}
},{
    field: 'kssj',
    title: '开始修复时间'
},{
    field: 'jssj',
    title: '结束时间'
},{
    field: 'wcbz',
    title: '完成标志',
    formatter: function(value, row, index) {
    	if(value=="0")
    		return "未完成";
    	else
    		return "已完成";
	}
},{
    field: 'cz',
    title: '操作',
    formatter: function(value, row, index) {
    	if(row.wcbz=="0")
    		return '<a href="#" onclick="opencpxfjldetail('+row.id+')">编辑</a>';
    	else
    		return '';
	}
}]